package com.jiuzhou.guanwang.jzcp.base;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.DownloadListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.jiuzhou.guanwang.jzcp.R;

public class WebFragment extends Fragment {
    Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            WebFragment.this.hideTittleBar();
        }
    };
    private boolean isClose;
    private LinearLayout loading;
    WebSettings mWebSettings;
    WebView mWebview;
    private String url;

    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_web, container, false);
        this.url = getArguments().getString("url");
        initView(view);
        return view;
    }

    private void initView(View view) {
        this.mWebview = (WebView) view.findViewById(R.id.web);
        this.loading = (LinearLayout) view.findViewById(R.id.ll_load);
        this.mWebSettings = this.mWebview.getSettings();
        this.mWebSettings.setJavaScriptEnabled(true);
        this.mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        this.mWebSettings.setDomStorageEnabled(true);
        this.mWebSettings.setUseWideViewPort(true);
        this.mWebSettings.setLoadWithOverviewMode(true);
        this.mWebSettings.setSupportZoom(true);
        this.mWebSettings.setBuiltInZoomControls(true);
        this.mWebSettings.setDisplayZoomControls(false);
        this.mWebview.loadUrl(this.url);
        this.mWebview.setDownloadListener(new DownloadListener() {
            public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
                WebFragment.this.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url)));
            }
        });
        this.mWebview.setWebChromeClient(new WebChromeClient() {
            public void onReceivedTitle(WebView view, String title) {
            }

            public void onProgressChanged(WebView view, int newProgress) {
            }
        });
        this.mWebview.setWebViewClient(new WebViewClient() {
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                WebFragment.this.loading.setVisibility(View.VISIBLE);
                if (!WebFragment.this.isClose) {
                    new Thread(new Runnable() {
                        public void run() {
                            WebFragment.this.isClose = true;
                            while (WebFragment.this.isClose) {
                                try {
                                    Thread.sleep(100);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                }
                                WebFragment.this.handler.sendEmptyMessage(1);
                            }
                        }
                    }).start();
                }
            }

            public void onPageFinished(WebView view, String url) {
                WebFragment.this.loading.setVisibility(View.GONE);
                WebFragment.this.hideTittleBar();
                WebFragment.this.isClose = false;
            }

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http:") || url.startsWith("https:")) {
                    return false;
                }
                try {
                    WebFragment.this.startActivity(new Intent("android.intent.action.VIEW", Uri.parse(url)));
                } catch (Exception e) {
                }
                return true;
            }

            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }
        });
    }

    public void onDestroy() {
        if (this.mWebview != null) {
            this.mWebview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            this.mWebview.clearHistory();
            ((ViewGroup) this.mWebview.getParent()).removeView(this.mWebview);
            this.mWebview.destroy();
            this.mWebview = null;
        }
        super.onDestroy();
    }

    public void onDestroyView() {
        if (this.mWebview != null) {
            this.mWebview.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            this.mWebview.clearHistory();
            ((ViewGroup) this.mWebview.getParent()).removeView(this.mWebview);
            this.mWebview.destroy();
            this.mWebview = null;
        }
        super.onDestroyView();
    }

    private void hideTittleBar() {
        String javascript = "javascript:function hideOther() {document.getElementsByClassName('ui-head')[0].style.display='none';}";
        if (this.mWebview != null) {
            this.mWebview.loadUrl(javascript);
            this.mWebview.loadUrl("javascript:hideOther();");
        }
    }
}